package com.example.agriapp.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

// FeedDbHelper.java
public class FeedDbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "agri_database.db";
    private static final int DATABASE_VERSION = 2;
    
    public FeedDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_FEEDS_TABLE = "CREATE TABLE " + 
            FeedContract.FeedEntry.TABLE_NAME + " (" +
            FeedContract.FeedEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            FeedContract.FeedEntry.COLUMN_USER_ID + " TEXT NOT NULL, " +
            FeedContract.FeedEntry.COLUMN_USERNAME + " TEXT NOT NULL, " +
            FeedContract.FeedEntry.COLUMN_AVATAR + " TEXT, " +
            FeedContract.FeedEntry.COLUMN_CONTENT + " TEXT NOT NULL, " +
            FeedContract.FeedEntry.COLUMN_DATE + " TEXT NOT NULL, " +
            FeedContract.FeedEntry.COLUMN_LIKES + " INTEGER DEFAULT 0, " +
            FeedContract.FeedEntry.COLUMN_COMMENTS + " INTEGER DEFAULT 0, " +
            FeedContract.FeedEntry.COLUMN_HAS_SHOP + " INTEGER DEFAULT 0, " +
            FeedContract.FeedEntry.COLUMN_IS_FOLLOWED + " INTEGER DEFAULT 0, " +
            FeedContract.FeedEntry.COLUMN_TAG + " TEXT, " +
            FeedContract.FeedEntry.COLUMN_IMAGE_URI + " TEXT);";
        
        db.execSQL(SQL_CREATE_FEEDS_TABLE);
        
        // 插入初始数据
        //insertInitialData(db);
    }
    
    private void insertInitialData(SQLiteDatabase db) {
        // 第一条动态
        ContentValues values1 = new ContentValues();
        values1.put(FeedContract.FeedEntry.COLUMN_USER_ID, "user_1533361");
        values1.put(FeedContract.FeedEntry.COLUMN_USERNAME, "153****3361");
        values1.put(FeedContract.FeedEntry.COLUMN_AVATAR, "default_avatar");
        values1.put(FeedContract.FeedEntry.COLUMN_CONTENT, "大家好");
        values1.put(FeedContract.FeedEntry.COLUMN_DATE, "2024-05-19");
        values1.put(FeedContract.FeedEntry.COLUMN_HAS_SHOP, 1);
        db.insert(FeedContract.FeedEntry.TABLE_NAME, null, values1);
        
        // 第二条动态
        ContentValues values2 = new ContentValues();
        values2.put(FeedContract.FeedEntry.COLUMN_USER_ID, "user_shangqiu");
        values2.put(FeedContract.FeedEntry.COLUMN_USERNAME, "商丘市菜篮子");
        values2.put(FeedContract.FeedEntry.COLUMN_AVATAR, "shangqiu_avatar");
        values2.put(FeedContract.FeedEntry.COLUMN_CONTENT, 
                   "商丘月老红缘相亲节目每个周六周日准时与你邂逅");
        values2.put(FeedContract.FeedEntry.COLUMN_DATE, "2024-05-18");
        values2.put(FeedContract.FeedEntry.COLUMN_LIKES, 1);
        values2.put(FeedContract.FeedEntry.COLUMN_HAS_SHOP, 1);
        values2.put(FeedContract.FeedEntry.COLUMN_TAG, "月老红缘");
        values2.put(FeedContract.FeedEntry.COLUMN_IMAGE_URI, "content://media/poster.jpg");
        db.insert(FeedContract.FeedEntry.TABLE_NAME, null, values2);
        
        // 第三条动态
        ContentValues values3 = new ContentValues();
        values3.put(FeedContract.FeedEntry.COLUMN_USER_ID, "user_shangqiu");
        values3.put(FeedContract.FeedEntry.COLUMN_USERNAME, "商丘市菜篮子");
        values3.put(FeedContract.FeedEntry.COLUMN_AVATAR, "shangqiu_avatar");
        values3.put(FeedContract.FeedEntry.COLUMN_CONTENT, "美丽的商丘古城");
        values3.put(FeedContract.FeedEntry.COLUMN_DATE, "2024-05-10");
        values3.put(FeedContract.FeedEntry.COLUMN_HAS_SHOP, 1);
        values3.put(FeedContract.FeedEntry.COLUMN_IMAGE_URI, "content://media/city.jpg");
        db.insert(FeedContract.FeedEntry.TABLE_NAME, null, values3);
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + FeedContract.FeedEntry.TABLE_NAME);
        onCreate(db);
    }
}